﻿package kliment.api.google {
	import com.adobe.serialization.json.JSONDecoder;
	import kliment.api.ITranslator;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	
	/**
     * Dispatched when open request.
     */
    [Event(name = 'open', type = 'flash.events.Event')]
	/**
     * Dispatched when translate complete.
     */
    [Event(name = 'complete', type = 'flash.events.Event')]
	/**
	 * ...
	 * @author	Kliment
	 * @version	1.2.5
	 * 
	 * see more: http://code.google.com/apis/ajaxlanguage/documentation/reference.html#_intro_fonje
	 * class name: kliment.api.google.GoogleTranslatorWord
	 */
	public class GoogleTranslatorWord extends EventDispatcher implements ITranslator {
		static private const _URL:String = 'http://ajax.googleapis.com/ajax/services/language/';
		static private const _TRANSLATE_URL:String = 'translate';
		static private const _DETECT_URL:String = 'detect';
		static private const _PROTOCOL_NUMBER:String = '1.0';
		private var _data:Object;
		
		public var method:String = URLRequestMethod.GET;
		public var format:String = 'text';
		public var key:String;
		
		public function GoogleTranslatorWord() {
			
		}
		
		public function translate(str:String, fromLang:String, toLang:String):void {
			var urlVars:URLVariables = new URLVariables();
			urlVars.q = str;
			urlVars.langpair = new Array(fromLang, toLang).join('|');
			
			_load(urlVars, _URL + _TRANSLATE_URL);
		}
		
		public function detect(str:String):void {
			var urlVars:URLVariables = new URLVariables();
			urlVars.q = str;
			
			_load(urlVars, _URL + _DETECT_URL);
		}
		
		private function _load(urlVars:URLVariables, url:String):void {
			urlVars.v =_PROTOCOL_NUMBER;
			urlVars.format = format;
			if (key.length > 0)
				urlVars.key = key;
			
			var urlLoader:URLLoader = new URLLoader();
			urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
			urlLoader.addEventListener(Event.OPEN, _onLoaderHandler);
			urlLoader.addEventListener(Event.COMPLETE, _onLoaderHandler);
			
			var urlRequest:URLRequest = new URLRequest(url);
			urlRequest.method = method;
			urlRequest.data = urlVars;
			urlLoader.load(urlRequest);
		}
		
		private function _onLoaderHandler(event:Event):void {
			switch(event.type) {
				case Event.COMPLETE:
					_data = new JSONDecoder(event.target.data as String, true).getValue() as Object;
				break;
			}
			dispatchEvent(event);
		}
		
		/**
		Response for Translate Language
			responseData.translatedText = the-translated-text
			responseData.detectedSourceLanguage = the-source-language
			responseDetails = null | string-on-error
			responseStatus = 200 | error-code
		
		
		Response for Detect Language
			responseData.language = the-detected-language
			responseData.isReliable = the-reliability-of-the-detect
			responseData.confidence = the-confidence-level-of-the-detect
			responseDetails = null | string-on-error
			responseStatus = 200 | error-code
		*/
		public function get data():Object { return _data }
		
	}

}